package com.archermind.empsys.dao.impl;

import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.archermind.empsys.dao.EmployeeDao;
import com.archermind.empsys.entity.Employee;
import com.archermind.empsys.utils.JdbcUtils;
/**
 * 员工实现类
 * @author Administrator
 *
 */
public class EmployeeDaoImpl implements EmployeeDao{
	
	public void add(Employee emp) {
		try{
			QueryRunner runner = new QueryRunner();
			String sql = "insert into employee (id,name,pwd,salary,work_years,state) values(?,?,?,?,?,?)";
			Object params[] = {emp.getId(),emp.getName(),emp.getPwd(),emp.getSalary(),emp.getWork_years(),emp.getState()};
			runner.update(JdbcUtils.getConnection(), sql, params);
		}catch (Exception e) {
			throw new RuntimeException();
		}
	}

	public void delete(String empId) {
		try {
			QueryRunner runner = new QueryRunner();
			String sql = "delete from  favourite where userID=? and ckID=?";
			Object params[] = { empId };
			runner.update(JdbcUtils.getConnection(), sql, params);

		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public Employee findById(String empId) {
		try{
			QueryRunner runner = new QueryRunner();
			String sql = "select * from employee where id=?";
			Object params[] = { empId };
			return (Employee) runner.query(JdbcUtils.getConnection(), sql, empId, new BeanHandler(Employee.class));
		}catch (Exception e) {
			throw new RuntimeException(e);
		}
		
	}

	public void update(Employee emp) {
		try{
			QueryRunner runner = new QueryRunner();
			String sql = "update employee set pwd=?,salary=?,work_years=?,state=? where id=?";
			Object params[] = {emp.getPwd(),emp.getSalary(),emp.getWork_years(),emp.getState(),emp.getId()};
			runner.update(JdbcUtils.getConnection(), sql, params);
		}catch (Exception e) {
			throw new RuntimeException(e);
		}
	}



	public List<Employee> getByTechnology(String tech) {
		try{
			QueryRunner runner = new QueryRunner();
			
			//首先根据技能从technoloty表查出技能id
			String sql = "select id from technology where tech_name=?";
			String tecId = (String) runner.query(JdbcUtils.getConnection(), sql, tech, new BeanHandler(Employee.class));
			System.out.println(tecId);
			//根据技能id从works表查出员工id
			
			
			//根据员工id查出员工信息
			return null;
		}catch (Exception e) {
			throw new RuntimeException(e);
		}
		
	}

	public Employee findByName(String name) {
		try{
			QueryRunner runner = new QueryRunner();
			String sql = "select * from employee where name=?";
			Object params[] = { name };
			return (Employee) runner.query(JdbcUtils.getConnection(), sql, params, new BeanHandler(Employee.class));
		}catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public List<Employee> getByState(String state) {
		try{
			QueryRunner runner = new QueryRunner();
			String sql = "select * from employee where state=?";
			return  (List) runner.query(JdbcUtils.getConnection(), sql, new BeanListHandler(Employee.class));
		}catch (Exception e) {
			throw new RuntimeException(e);
		}
	}


}
